﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.WPF.Util;
using Bling.Core;
using Bling.Util;
using Bling.Physics.UI;
using Bling.Physics;
using Bling.Properties;
using Bling.Mixins;
using Bling.Blocks;
using Bling.Shapes;
using Bling.Angles;

namespace Bling.Tutorial {
  public class PhysicsChain : PhysicsCanvas {
    public class ThumbBlock : TopBlock<ThumbBlock, ThumbBl>, HasPosition.IHas<ThumbBlock> {
      public ThumbBlock(BlockManager Manager, CanvasBl Canvas, int Count)
        : base(Manager, Count, (block, idx) => {
          var thumb = new ThumbBl(Canvas) {
            Background = idx.SelectColor(),
            CanDrag = true,
            Size = 20d,
          };
          thumb.Opacity = thumb.Mouse.IsOver.Condition(1d, .1d.Bl());
          return thumb;
        }) { }
    }
    protected override Action Init(CanvasBl MainCanvas, BlockManager Manager) { // executes before blocks are initialized.
      MainCanvas.Background = Brushes.White;
      var slider = new SliderBl(MainCanvas) {
        LeftTop = 0,
        Width = MainCanvas.Width,
        Minimum = 0d,
        Maximum = 100d,
        Value = 10d,
      };
      {
        var canvas = new CanvasBl(MainCanvas) {
          Size = MainCanvas.Size - new Double2Bl(0d, slider.Bottom),
          Left = 0, Top = slider.Bottom, 
        };
        var lbl0 = new LabelBl(canvas) {
          LeftTop = 0,
          Font = { Weight = FontWeights.SemiBold, Family = "Courier New".Convert<FontFamily>() },
          Content = "Elem.Position().InstallVerletStep(.01);",
        };
        var lbl1 = new LabelBl(canvas) {
          LeftTop = lbl0.LeftBottom,
          Font = { Weight = FontWeights.SemiBold, Family = "Courier New".Convert<FontFamily>() },
          Content = "Elem.Position().Distance(Elem.Prev[1].Position()).Relax.To = ".Bl() + slider.Value.ToStringBl(100d) + ";",
        };
        canvas.RenderTransform[0d].Scale = MainCanvas.Width / lbl1.Width;
      }

      var Block = new ThumbBlock(Manager, MainCanvas, 100);
      var r = new Random();
      Block.ForAll = Elem => {
        Elem.Position().Init = Double2Bl.RandomFrom(r) * 500;
        Elem.Position().Link[!Elem.At.IsDragging] = Elem.At.CenterPosition;
        Elem.Position().Relax[.5d] = Elem.Position().Clamp(new Double2Bl(0, 0), MainCanvas.Size);
        Elem.Position().InstallVerletStep(.01);
        Elem.Position().Distance(Elem.Prev[1].Position()).Relax.To = slider.Value;
      };
      return () => {
        var shape = new PathShapeBl(MainCanvas) {
          IsHitTestVisible = false,
          Stroke = { Thickness = 2d, Brush = Brushes.Black },
        };
        shape.Start = Block[0].At.CenterPosition;
        for (int i = 1; i < Block.Count; i++) {
          shape.Segments.Add(new LineSegmentBl() { Point = Block[i].At.CenterPosition });
        }


      };
    }

  }
}